package com.cn.bukeManage.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.annotations.Param;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.support.odps.udf.JSONSet;
import com.cn.bukeManage.pojo.Activity;
import com.cn.bukeManage.pojo.Comm;
import com.cn.bukeManage.pojo.Composition;
import com.cn.bukeManage.pojo.Location;
import com.cn.bukeManage.pojo.Picture;
import com.cn.bukeManage.pojo.Sample;
import com.cn.bukeManage.pojo.Style;
import com.cn.bukeManage.pojo.Territory;
import com.cn.bukeManage.pojo.page.SamplePage;
import com.cn.bukeManage.service.ShoppingmallService;
//鏉╂瑦妲搁崯鍡楃厔缁狅紕鎮�
@Controller
@RequestMapping("/shoppingmall")
public class ShoppingmallController {
	@Resource
	private ShoppingmallService shoppingmallService;
	Logger logger = Logger.getLogger(this.getClass());
	@RequestMapping("/goods")
	public String goods(Model model,Integer currentPage,Integer state){
		List<SamplePage> SampleList= shoppingmallService.getAllGoodsByPage(model,currentPage,state);
		model.addAttribute("oneMenu", "shoppingmall");
		model.addAttribute("twoMenu", "goods");
		model.addAttribute("SampleList",SampleList);
		model.addAttribute("type",9);
		return "shoppingmall/goods";
	}
	//鍒嗙被鏄剧ず鍟嗗搧
	@RequestMapping("/goodsByType")
	public String goodsByType(Model model,Integer currentPage,Integer type,Integer state){
		List<SamplePage> SampleList= shoppingmallService.getAllGoodsByPageAndType(model,currentPage,type,state);
		model.addAttribute("oneMenu", "shoppingmall");
		model.addAttribute("twoMenu", "goods");
		model.addAttribute("SampleList",SampleList);
		model.addAttribute("type",type);
		return "shoppingmall/goods";		
	}   
	
	
	
	
	//鐠哄啿鍩屾稉濠佺炊閸熷棗鎼�
	@RequestMapping("/toAddGoods")
	public String toAddGoods(Model model){
		model.addAttribute("oneMenu", "shoppingmall");
		model.addAttribute("twoMenu", "goods");
		return "shoppingmall/addGoods";
	}
	
	//濞茶濮╃粻锛勬倞
	@RequestMapping("/activity")
	public String activity(Model model,Integer currentPage){
		List<Activity> activities=  shoppingmallService.getAllActivityByPage(model,currentPage);
		model.addAttribute("oneMenu", "shoppingmall");
		model.addAttribute("twoMenu", "activity");
		model.addAttribute("activities", activities);
		return "shoppingmall/activity";
	}
	//閺嶈宓佸ú璇插Зid閼惧嘲褰囬幍锟介張澶嬫た閸斻劎娈戠拠锔剧矎娣団剝浼�
	@RequestMapping("/getActivityById")
	@ResponseBody
	public Activity getActivityById(Integer activityId){
		Activity activity=shoppingmallService.getActivityById(activityId);
		return activity;
	}
	//娣囶喗鏁煎ú璇插З
	@RequestMapping("updateActivity")
	@ResponseBody
	public Integer updateActivity(Activity activity){
		Integer integer=shoppingmallService.updateActivity(activity);
		return integer;
	}
	//閺傛澘顤冨ú璇插З
	@RequestMapping("addActivity")
	@ResponseBody
	public Integer addActivity(Activity activity){
		Integer integer=shoppingmallService.addActivity(activity);
		return integer;		
	}
	//閸掔娀娅庡ú璇插З
	@RequestMapping("/deleteActivity")
	@ResponseBody
	public Integer deleteActivity(Integer activityId){
		Integer integer = shoppingmallService.deleteActivity(activityId);
		return integer;
	}
	//妞嬪孩鐗哥粻锛勬倞
	@RequestMapping("/style")
	public String style(Model model,Integer currentPage){
		List<Style> styles=shoppingmallService.getAllStyleByPage(model,currentPage);
		model.addAttribute("oneMenu", "shoppingmall");
		model.addAttribute("twoMenu", "style");
		model.addAttribute("styles", styles);
		return "shoppingmall/style";
	}
	//閫氳繃椋庢牸id寰楀埌椋庢牸淇℃伅
	@RequestMapping("getStyleById")
	@ResponseBody
	public Style getStyleById (Integer styleId){
		Style style=shoppingmallService.getStyleById(styleId);
		return style;
	}
	//淇敼椋庢牸
	@RequestMapping("/updateStyle")
	@ResponseBody
	public Integer updateStyle(Style style){
		Integer integer=shoppingmallService.updateStyle(style);
		return integer;
	}
	//娣诲姞椋庢牸顤�
	@RequestMapping("/addStyle")
	@ResponseBody
	public Integer addStyle(Style style,HttpServletResponse response){
		response.setHeader("Access-Control-Allow-Origin", "*");  
	    response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");  
	    response.setHeader("Access-Control-Max-Age", "3600");  
	    response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
		Integer integer=shoppingmallService.addStyle(style);
		return integer;
	}
	//鍒犻櫎椋庢牸
	@RequestMapping("/deleteStyle")
	@ResponseBody
	public Integer deleteStyle(Integer styleId){
		Integer integer = shoppingmallService.deleteStyle(styleId);
		return integer;
	}
	//娴ｈ法鏁ら崷鐑樻珯缁狅紕鎮�
	@RequestMapping("/location")
	public String location(Model model,Integer currentPage){
		List<Location> locations=shoppingmallService.getAllLocationByPage(model,currentPage);
		model.addAttribute("oneMenu", "shoppingmall");
		model.addAttribute("twoMenu", "location");
		model.addAttribute("locations", locations);
		return "shoppingmall/location";
	}
	@RequestMapping("/getLocationById")
	@ResponseBody
	public Location getLocationById(Integer locationId){
		Location location=shoppingmallService.getLocationById(locationId);
		return location;
	}
	@RequestMapping("/saveLocation")
	@ResponseBody
	public Integer saveLocation(Location location){
		Integer integer=shoppingmallService.updateLocation(location);
		return integer;
	}
	//閸掔娀娅庨柅鍌滄暏閸︾儤娅�
	@RequestMapping("/deleteLocation")
	@ResponseBody
	public Integer deleteLocation(Integer locationId){
		Integer integer = shoppingmallService.deleteLocation(locationId);
		return integer;
	}
	@RequestMapping("/addLocation")
	@ResponseBody
	public Integer addLocation(Location location,HttpServletResponse response){
		response.setHeader("Access-Control-Allow-Origin", "*");  
	    response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");  
	    response.setHeader("Access-Control-Max-Age", "3600");  
	    response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
		Integer integer=shoppingmallService.addLocation(location);
		return integer;
	}

	//閸熷棗鎼х猾璇插焼缁狅紕鎮�
	@RequestMapping("/type")
	public String type(Model model,Integer currentPage){
		List<Map<String,Object>> maps=shoppingmallService.getAllType();
		model.addAttribute("oneMenu", "shoppingmall");
		model.addAttribute("twoMenu", "type");
		model.addAttribute("maps",maps);
		return "shoppingmall/type";
	}
	//瀵拷闁艾灏崺鐔侯吀閻烇拷
	@RequestMapping("/territory")
	public String territory(Model model,Integer currentPage){
		List<Territory> territories=shoppingmallService.getAllTerritoriesByPage(model,currentPage);
		model.addAttribute("oneMenu", "shoppingmall");
		model.addAttribute("twoMenu", "territory");
		model.addAttribute("territories", territories);
		return "shoppingmall/territory";
	}

	
		//鑾峰彇椋庢牸鍜屽畨瑁呬綅缃�
		@RequestMapping("/tianjiaStyleLocation")
		@ResponseBody
		public Map<String, Object> tianjiaStyleLocation(HttpServletResponse response){
			
			response.setHeader("Access-Control-Allow-Origin", "*");  
		    response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");  
		    response.setHeader("Access-Control-Max-Age", "3600");  
		    response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
			List<Style> styles=shoppingmallService.getAllStyle();
			List<Location> locations = shoppingmallService.getAllLocation();
			Map<String, Object> map = new HashMap<>();
			map.put("styles", styles);
			map.put("locations", locations);
			return map;
		}
		
	//濞村鐦�
	@RequestMapping("/test")
	public String text(Model model){
		model.addAttribute("oneMenu", "shoppingmall");
		model.addAttribute("twoMenu", "territory");
		return "shoppingmall/xiangqing";
	}

	
	//濞村鐦�
	@RequestMapping("/test2")
	public String text2(Model model,String classElements){
		System.out.println(classElements);
		model.addAttribute("oneMenu", "shoppingmall");
		model.addAttribute("twoMenu", "territory");
		model.addAttribute("classElements",classElements);
		return "shoppingmall/xiangqing2";
	}
	
	//鍒ゆ柇绯诲垪鍙锋槸鍚﹂噸澶�
		@RequestMapping("/select_serialnumber")
		@ResponseBody
		public String select_serialnumber(String serialnumber,HttpServletResponse response){
			response.setHeader("Access-Control-Allow-Origin", "*");  
		    response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");  
		    response.setHeader("Access-Control-Max-Age", "3600");  
		    response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
			return shoppingmallService.select_serialnumber(serialnumber);
		}
	
		//鑾峰彇閰嶄欢绫诲瀷
		@RequestMapping("/peijianstyle")
		@ResponseBody
		public Map<String, Object> peijianstyle(HttpServletResponse response){
			response.setHeader("Access-Control-Allow-Origin", "*");  
		    response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");  
		    response.setHeader("Access-Control-Max-Age", "3600");  
		    response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
			Map<String, Object> map = new HashMap<>();
			map.put("acc", shoppingmallService.getAllAccTyp());
			return map;
		}
		
		
		//鑾峰彇鎴愬搧甯樼被鍨�
		@RequestMapping("/chengpinlianstyle")
		@ResponseBody
		public Map<String, Object> chengpinlianstyle(HttpServletResponse response){
			response.setHeader("Access-Control-Allow-Origin", "*");  
		    response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");  
		    response.setHeader("Access-Control-Max-Age", "3600");  
		    response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
			Map<String, Object> map = new HashMap<>();
			map.put("finType", shoppingmallService.getAllFinType());
			return map;
		}
		
		@RequestMapping("/select_sample")
		public String  select_samplebyserialnumber(Integer serialnumber,Integer state,HttpServletResponse response,Model modelmap){
			response.setHeader("Access-Control-Allow-Origin", "*");  
		    response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");  
		    response.setHeader("Access-Control-Max-Age", "3600");  
		    response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
		    List<SamplePage> SampleList=shoppingmallService.select_samplebyserialnumber(modelmap,serialnumber,state);
		    modelmap.addAttribute("oneMenu", "shoppingmall");
		    modelmap.addAttribute("twoMenu", "goods");
		    modelmap.addAttribute("SampleList",SampleList);
			//璁╃晫闈㈠浐瀹歴elect涓嬫媺妗�
		    modelmap.addAttribute("type",9);
			return "shoppingmall/goods";	
		}
	
		//鍒颁慨鏀瑰晢鍝侀〉闈�
		@RequestMapping("/upGoods")
		public String toUpdateGoods(Model modelmap,Integer sampleId,HttpServletResponse response){
			
			response.setHeader("Access-Control-Allow-Origin", "*");  
			response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");  
			response.setHeader("Access-Control-Max-Age", "3600");  
			response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
			modelmap.addAttribute("oneMenu", "shoppingmall");
			modelmap.addAttribute("twoMenu", "goods");
			Map<String,Object> map=new HashMap<>();
			map.put("sampleId", sampleId);
			//鑾峰彇鍟嗗搧sample锛屼緵搴斿晢 鎴愬搧甯樼被鍨� 鍜岄厤浠剁被鍨�
			modelmap.addAttribute("sample",shoppingmallService.getSampleBySampleId(map,modelmap));
			//闈㈡枡浣跨敤鏂瑰紡
			modelmap.addAttribute("compositions",shoppingmallService.findCompositionBySampleId(sampleId));
			//鑾峰彇鎵�鏈夐鏍�
			modelmap.addAttribute("styles", shoppingmallService.getAllStyle());
			//鑾峰彇鎵�鏈夐�傜敤鍦烘櫙
			modelmap.addAttribute("locations", shoppingmallService.getAllLocation());
			//鑾峰彇椋庢牸涓棿琛�
			modelmap.addAttribute("sample_styles", shoppingmallService.getAllsample_stylebysampleId(sampleId));
			//鑾峰彇閫傜敤鍦烘櫙涓棿琛�
			modelmap.addAttribute("sample_locations", shoppingmallService.getAllsample_locationsampleId(sampleId));
			//鑾峰彇璇︽儏鍥�
			modelmap.addAttribute("picture", shoppingmallService.findPicBySampleId(sampleId));	
			return "shoppingmall/upGoods";
		}
		
		/**
		 * 修改商品
		 * @param sample
		 * @param supplier
		 * @param properties
		 * @param compositions
		 * @param assist
		 * @param request
		 * @param response
		 * @return
		 */
		@RequestMapping("/updategoodsubmit")
		@ResponseBody
		public String updateSample(String sample,String supplier, String properties,String compositions,String assist,HttpServletRequest request,HttpServletResponse response){
			//前端传过来的数据
			Enumeration enu=request.getParameterNames();  
			while(enu.hasMoreElements()){  
			String paraName=(String)enu.nextElement();  
			System.out.println("信息"+paraName+": "+request.getParameter(paraName));  
			}
			//去除数据中的标签
			if(assist!=null){
				assist=assist.replaceAll("<span>","").replaceAll("</span>","").replaceAll(",","");
			}
			JSONArray c = JSONArray.parseArray(assist);
			List<Object>list=new ArrayList<Object>();
			for(Object a:c){
				if(!list.contains(a)){
				list.add(a);
				}
			}
			System.out.println("list:"+list);
			
			
			
			
			//解决跨域问题
			response.setHeader("Access-Control-Allow-Origin", "*");  
		    response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");  
		    response.setHeader("Access-Control-Max-Age", "3600");  
		    response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
			try {
				return shoppingmallService.updateSample(sample, supplier, properties, compositions, assist, request);
			} catch (Exception e) {
				e.printStackTrace();
				return "2";
			}
		}
		
		
		
		/**
		 * 添加商品
		 * @param sample
		 * @param supplier
		 * @param properties
		 * @param compositions
		 * @param assist
		 * @param request
		 * @param response
		 * @return
		 */
		
		@RequestMapping("/addgoods.do")
		@ResponseBody
		public String addgoods(String sample, String supplier,String properties,String compositions,String assist,
				HttpServletRequest request,HttpServletResponse response){
			
			//解决跨域问题
			response.setHeader("Access-Control-Allow-Origin", "*");  
		    response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");  
		    response.setHeader("Access-Control-Max-Age", "3600");  
		    response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
		    //前端取的数据
		    Enumeration enu=request.getParameterNames();
			while(enu.hasMoreElements()){  
			String paraName=(String)enu.nextElement();  
			System.out.println("信息："+paraName+": "+request.getParameter(paraName));  
			}
			//发过来的参数带有标签，所以把标签给去掉
			if(assist!=null){
				assist=assist.replaceAll("<span>","").replaceAll("</span>","").replaceAll("<br>","");
			}
			
			try {
				return shoppingmallService.addgoods(sample,supplier,properties,compositions,assist,request);
			} catch (Exception e) {
				e.printStackTrace();
				return "2";
			}
	 
		}

			@RequestMapping("/findgoods.do")
			public String findgoods(Model modelmap, Integer sampleId,HttpServletResponse response){
				response.setHeader("Access-Control-Allow-Origin", "*");  
			    response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");  
			    response.setHeader("Access-Control-Max-Age", "3600");  
			    response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
			    Sample sample = shoppingmallService.selectSampleBySampleId(sampleId);
			    modelmap.addAttribute("sample", sample);
				return "shoppingmall/findGoods";
			}
			@RequestMapping("/toOldGoods")
			public String toOldGoods(Model model,Integer currentPage,Integer state){
				List<SamplePage> SampleList= shoppingmallService.getAllGoodsByPage(model,currentPage,state);
				model.addAttribute("oneMenu", "shoppingmall");
				model.addAttribute("twoMenu", "goods");
				model.addAttribute("SampleList",SampleList);
				model.addAttribute("type",9);
				return "shoppingmall/oldgoods";
			}
			//鍒嗙被鏄剧ず鍟嗗搧
			@RequestMapping("/toOldGoodsByType")
			public String OldGoodsByType(Model model,Integer currentPage,Integer type,Integer state){
				System.out.println(type);
				List<SamplePage> SampleList= shoppingmallService.getAllGoodsByPageAndType(model,currentPage,type,state);
				model.addAttribute("oneMenu", "shoppingmall");
				model.addAttribute("twoMenu", "goods");
				model.addAttribute("SampleList",SampleList);
				model.addAttribute("type",type);
				return "shoppingmall/oldgoods";		
			}
			
			
			@RequestMapping("/upgoodsState.do")
			@ResponseBody
			public String updategoods(Integer sampleId,Integer state){
				try{
					shoppingmallService.updateGoodsByStateAndSampleId(sampleId, state);
					return "0";
				}catch(Exception e){
					e.printStackTrace();
					return "1";
				}
					
			}
			
			@RequestMapping("/toState")
			public String toStateGoods(Model model,HttpServletResponse response){
				response.setHeader("Access-Control-Allow-Origin", "*");  
			    response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");  
			    response.setHeader("Access-Control-Max-Age", "3600");  
			    response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
				return "shoppingmall/state";
			}
			
			
			
			@RequestMapping("/toAllGoods")
			public String toAllGoods(Model model,Integer currentPage){
				List<SamplePage> SampleList= shoppingmallService.getGoodsByPage(model,currentPage);
				model.addAttribute("oneMenu", "shoppingmall");
				model.addAttribute("twoMenu", "goods");
				model.addAttribute("SampleList",SampleList);
				model.addAttribute("type",9);
				return "shoppingmall/allgoods";
			}
			
			@RequestMapping("/toAllGoodsByType")
			public String toAllGoodsByType(Model model,Integer currentPage,Integer type,Integer state){
				List<SamplePage> SampleList= shoppingmallService.getGoodsByPageAndType(model,currentPage,type);
				model.addAttribute("oneMenu", "shoppingmall");
				model.addAttribute("twoMenu", "goods");
				model.addAttribute("SampleList",SampleList);
				model.addAttribute("type",type);
				return "shoppingmall/allgoods";		
			}
			

			@RequestMapping("/select_samplebyId")
			public String  select_samplebyId(Integer serialnumber,HttpServletResponse response,Model modelmap){
				response.setHeader("Access-Control-Allow-Origin", "*");  
			    response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");  
			    response.setHeader("Access-Control-Max-Age", "3600");  
			    response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
			    List<SamplePage> SampleList=shoppingmallService.select_samplebyId(modelmap,serialnumber);
			    modelmap.addAttribute("oneMenu", "shoppingmall");
			    modelmap.addAttribute("twoMenu", "goods");
			    modelmap.addAttribute("SampleList",SampleList);
				
			    modelmap.addAttribute("type",9);
			    return "shoppingmall/allgoods";	
			}
			@RequestMapping("/deleteGoods")
			@ResponseBody
			public String deleteGoods(Integer sampleId){
				try{
				shoppingmallService.deleteGoods(sampleId);
				return "0";
				}catch(Exception e){
					e.printStackTrace();
					return "1";
				}
				
			}
			@RequestMapping("/getSampleById.do")
			@ResponseBody
			public Sample getSampleById(Integer sampleId,HttpServletResponse response){
				 Sample list = shoppingmallService.selectSampleBySampleId(sampleId);
				if(list!=null){
					return list;
				}else{
					return null;
				}
				
			}
			
			@RequestMapping("/toAddComposition")
			public String toAddComposition(Integer sampleId,Model modelmap){
				modelmap.addAttribute("sampleId", sampleId);
				return "shoppingmall/addBuLiao";
			}
			
			
			
			//添加布料
			@RequestMapping("/addComposition")
			@ResponseBody
			public String upComposition(String composition,HttpServletRequest request,HttpServletResponse response){
				response.setHeader("Access-Control-Allow-Origin", "*");  
			    response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");  
			    response.setHeader("Access-Control-Max-Age", "3600");  
			    response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
			    Enumeration enu=request.getParameterNames();  
				while(enu.hasMoreElements()){  
				String paraName=(String)enu.nextElement();  
				System.out.println("信息"+paraName+": "+request.getParameter(paraName));  
				}
				System.out.println(composition);
				try{
					shoppingmallService.insert_compositions(composition);
				return "0";
				}catch(Exception e){
					e.printStackTrace();
					return "1";
				}	
			}
			//删除布料
			@RequestMapping("/deleteCompositionByType")
			@ResponseBody
			public String deleteCompositionByType(Integer sampleId,Integer type,HttpServletRequest request,HttpServletResponse response){
				response.setHeader("Access-Control-Allow-Origin", "*");  
			    response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");  
			    response.setHeader("Access-Control-Max-Age", "3600");  
			    response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
			    System.out.println(sampleId+""+type);
				try{
					shoppingmallService.deleteCompositionByType(sampleId, type);
					return "0";
				}catch(Exception e){
					return "1";
				}
						
			}	
	}
